<template>
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap topbarwrap">
      <!-- 左侧标题 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧用户信息导航 -->
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" class="avatar" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <!-- 转换登录状态 -->
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" @click="changeIsShowLoginModal(true)" v-else>
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState,mapActions } from "vuex";
import { WeixinLoginApi } from "@/request/api";
export default {
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncIsShowToast: "showToast/asyncIsShowToast",
      asyncChangeUserInfoAndCartTotal:"userInfo/asyncChangeUserInfoAndCartTotal"
    })
  },
  created() {
    // 在router初始化完毕后在获取code
    this.$router.onReady(async () => {
      
      let mycode = this.$route.query.code;
      if (mycode) {
        let res = await WeixinLoginApi({
          //有code去换token
          code: mycode,
        });
        console.log(res);
        if (res.code == 0) {
          this.asyncIsShowToast({
            msg:'微信扫码登录成功',
            type:'success'
          })
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          this.changeIsLogined(true);
          // 更新用户信息
          this.asyncChangeUserInfoAndCartTotal()
          this.$router.push(this.$route.path);
        } else if (res.code == 400) {
          alert("请重新扫码");
          this.changeIsShowLoginModal(true);
        } else if (res.code == 407) {
          // 提示用户再进行手机号登录
          alert("请再进行一次手机号登录");
          this.changeIsShowLoginModal(true);
          localStorage.setItem("uuid", res.uuid);
          // 清除地址栏code
          this.$router.push(this.$route.path);
        }
      } else {
        //  非扫码登录
        // 更新登录状态
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(token));
        if (token) {
          this.asyncChangeUserInfoAndCartTotal()
        } else {
          this.initUserInfo();
        }
      }
    });
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background-color: #242b39;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    .right {
      li {
        float: left;
        margin-left: 20px;
        text-align: center;
        cursor: pointer;
        &.login-btn {
          width: 124px;
          background: #0a328e;
        }
      }
      .avatar {
        width: 23px;
        height: 23px;
        border-radius: 50%;
        vertical-align: middle;
      }
    }
  }
  .cart-btn {
    width: 124px;
    height: 40px;
    background: #0a328e;
    text-align: center;
    img {
      width: 20px;
      height: 19px;
      vertical-align: middle;
    }
    span {
      margin: 0 8px;
    }
    strong {
      width: 22px;
      height: 22px;
      line-height: 22px;
      display: inline-block;
      border-radius: 50%;
      background-color: #fd604d;
    }
  }
}
</style>